<?php
// +----------------------------------------------------------------------
// | Qbt CMS Plugin
// +----------------------------------------------------------------------
// | Copyright (c) 2012-2018 http://www.qbt8.com All rights reserved.
// +----------------------------------------------------------------------
// | Author: 黄老邪 <develop02@qbt8.com>
// +----------------------------------------------------------------------
namespace plugins\qbt_advertising;
use cmf\lib\Plugin;
use think\Config;
use think\db;

class QbtAdvertisingPlugin extends Plugin
{

    public $info = [
        'name'        => 'QbtAdvertising',
        'title'       => '广告管理',
        'description' => '千佰特广告管理应用',
        'status'      => 1,
        'author'      => '千佰特',
        'version'     => '1.0',
        'demo_url'    => 'http://www.qbt8.com',
        'author_url'  => 'http://www.qbt8.com'
    ];

    public $hasAdmin = 1;//插件是否有后台管理界面

            // `ad_width`  smallint(5) NULL DEFAULT '0' COMMENT '位置宽度' ,
            // `ad_height`  smallint(5) NULL DEFAULT '0' COMMENT '位置高度' ,
    // 插件安装
    public function install()
    {
        $config = Config::get('database');
        Db::query("
            CREATE TABLE IF NOT EXISTS `{$config['prefix']}ad_position` (
            `id`  int NOT NULL AUTO_INCREMENT,
            `media_type`  tinyint(3) NULL DEFAULT '0' COMMENT '广告位类型' ,
            `position_name`  varchar(100) NULL DEFAULT '' COMMENT '广告位名称' ,
            `ad_logo`  varchar(100) NULL DEFAULT '' COMMENT '广告位标识' ,
            `position_desc`  varchar(255) NULL DEFAULT '' COMMENT '广告位描述' ,
            PRIMARY KEY (`id`)
            )
        ");
        Db::query("
            CREATE TABLE IF NOT EXISTS `{$config['prefix']}ad` (
            `id`  int NOT NULL AUTO_INCREMENT,
            `position_id` int(11) NOT NULL COMMENT '位置id',
            `ad_name`  varchar(100) NULL DEFAULT '' COMMENT '广告名称' ,            
            `ad_link`  varchar(100) NULL DEFAULT '' COMMENT '广告链接' ,
            `ad_code`  text NULL COMMENT '自定义代码' ,
            `ad_text`  text NULL COMMENT '广告内容' ,
            `start_time`  int(11) NULL COMMENT '开始时间' ,
            `end_time`  int(11) NULL COMMENT '结束时间' ,
            `ad_img`  varchar(100) NULL DEFAULT '' COMMENT '缩略图' ,
            `enabled`  tinyint(3) NULL DEFAULT '1' COMMENT '是否开启' ,
            PRIMARY KEY (`id`)
            )
        ");

        $data['parent_id'] = 0;
        $data['type'] = 0;
        $data['status'] = 1;
        $data['icon'] = 'user';
        $data['app'] = 'plugin/QbtAdvertising';
        $data['controller'] = 'AdminIndex';
        $data['action'] = 'index';
        $data['name'] = '广告管理';
        $data['remark'] = '千佰特广告管理';
        $id = Db::table('qbtcmf_admin_menu')->insertGetId($data);
        $data1['parent_id'] = $id;
        $data1['type'] = 0;
        $data1['status'] = 1;
        $data1['app'] = 'plugin/QbtAdvertising';
        $data1['controller'] = 'AdminIndex';
        $data1['action'] = 'index';
        $data1['name'] = '广告列表';
        $data1['remark'] = '千佰特广告管理';
        Db::table('qbtcmf_admin_menu')->insert($data1);
        $data2['parent_id'] = $id;
        $data2['type'] = 0;
        $data2['status'] = 1;
        $data2['app'] = 'plugin/QbtAdvertising';
        $data2['controller'] = 'AdminIndex';
        $data2['action'] = 'ads_index';
        $data2['name'] = '广告位置';
        $data2['remark'] = '千佰特广告管理';
        Db::table('qbtcmf_admin_menu')->insert($data2);

        return true;
    }

    // 插件卸载
    public function uninstall()
    {
        $config = Config::get('database');
        Db::query("DROP TABLE `{$config['prefix']}ad_position`");
        Db::query("DROP TABLE `{$config['prefix']}ad`");
        return true;
    }


}